package com.wuage.infrastructure.exception;

import com.wuage.infrastructure.utils.ParameterFormatter;

/**
 * 错误信息接口
 *
 * @author shynin
 */
public interface ResultCode {
    /**
     * 获取错误码
     *
     * @return 错误码
     */
    Integer getCode();

    /**
     * 获取错误详情
     *
     * @return 错误详情
     */
    String getDesc();

    default BizException toException(String customMsg) {
        return new BizException(this.getCode(), customMsg);
    }

    default BizException toException() {
        return new BizException(this);
    }

    default BizException toException(String customMsg, Object... objects) {
        if (objects != null && objects.length != 0) {
            String msg = ParameterFormatter.format(customMsg, objects);
            if (objects[objects.length - 1] instanceof Throwable) {
                Throwable throwable = (Throwable)objects[objects.length - 1];
                msg = String.format("%s (%s)", msg, throwable.getClass().getSimpleName());
            }

            return this.toException(msg);
        } else {
            return this.toException(customMsg);
        }
    }
}
